package com.pharmarket.fr.androidpharmarket;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.ArrayList;


public class MainActivity extends Activity {

    private LinearLayout        mLinearScroll;
    private ListView            mListView;
    private ArrayList<String>   mArrayListArticles;
    private ArrayList<String>   mArrayListArticlesTemp;
    private String restURL = "http://localhost/test.php";

    int                         rowSize = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new RestOperation().execute(restURL);

        mLinearScroll = (LinearLayout) findViewById(R.id.linear_scroll);
        mListView = (ListView) findViewById(R.id.listArticles);
        mArrayListArticles = new ArrayList<String>();
        mArrayListArticlesTemp = new ArrayList<String>();

        for (int i = 1; i <= 30; i++)
        {
            mArrayListArticles.add("Apple"+i);
        }

        int rem = mArrayListArticles.size() % rowSize;
        if (rem > 0) {
            for (int i = 0; i < rowSize - rem; i++) {
                mArrayListArticles.add("");
            }
        }
        addItem(0);
        int size = mArrayListArticles.size() / rowSize;

        for (int j = 0; j < size; j++)
        {
            final int k;
            k = j;
            final Button btnPage = new Button(MainActivity.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(120, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(5, 2, 2, 2);
            btnPage.setTextColor(Color.WHITE);
            btnPage.setTextSize(26.0f);
            btnPage.setId(j);
            btnPage.setText(String.valueOf(j + 1));
            btnPage.setBackgroundColor(Color.GRAY);
            mLinearScroll.addView(btnPage, lp);
            btnPage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addItem(k);
                }
            });
        }
    }

    private void addItem(int i) {
        mArrayListArticlesTemp.clear();
        i = i * rowSize;
        for (int j = 0; j < rowSize; j++) {
            mArrayListArticlesTemp.add(j, mArrayListArticles.get(i));
            i = i + 1;
        }
        setView();
    }

    private void setView() {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this,
                android.R.layout.simple_list_item_1,
                mArrayListArticlesTemp
        );
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     Log.e("Article", mArrayListArticlesTemp.get(position));
                 }
             }
        );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public class RestOperation extends AsyncTask<String, Void, Void> {

        final HttpClient httpClient = new DefaultHttpClient();
        String content;
        String error;
        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        String data;
        String serverDataReceived;
        String showParsedJSON;

        @Override
        public void onPreExecute() {
            super.onPreExecute();

            progressDialog.setTitle("Please wait ...");
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }
    }
}
